trigger UpdateDashboard on Achieve__c (after insert,after update,after delete) {
    Integer YearI = Date.today().year();
    String Year;
    Integer MonthI = Date.today().month();
    String Month;
    
    if(MonthI == 1){
        Year = String.valueof(YearI - 1);
        Month = String.valueof(12);
    }
    else{
        Year = String.valueof(YearI);
        Month = String.valueof(MonthI - 1);
    }
    
    if(system.trigger.isInsert){
        if((Trigger.new.size() == 1)&&(Trigger.new[0].Month__c != Month)){
            Achieve__c NewRecord = Trigger.new[0];
            List<MedicalDashboard__c> NeedRecordList = [Select Id,Brand_use_in_hospital_cover_actual__c,Brand_use_in_hospital_cover_forecast__c,HCP_evaluation_cover_actual__c,
                HCP_evaluation_cover_forecast__c,P_class_cover_actual__c,P_class_cover_forecast__c,total_conversion_actual__c,
                total_conversion_forecast__c,year__c,month__c From MedicalDashboard__c Where ownerid =: Trigger.new[0].ownerid and year__c =: Trigger.new[0].Year__c and month__c =: Trigger.new[0].Month__c];
            if(NeedRecordList.size() > 0){
                MedicalDashboard__c NeedRecord = NeedRecordList.get(0);
                
                if(NeedRecord.Brand_use_in_hospital_cover_actual__c > 0)
                    NeedRecord.Brand_use_in_hospital_cover_actual__c = NeedRecord.Brand_use_in_hospital_cover_actual__c + NewRecord.Brand_use_in_hospital_cover_achieve__c;
                else
                    NeedRecord.Brand_use_in_hospital_cover_actual__c =  NewRecord.Brand_use_in_hospital_cover_achieve__c;
                if(NeedRecord.Brand_use_in_hospital_cover_forecast__c > 0)
                    NeedRecord.Brand_use_in_hospital_cover_forecast__c = NeedRecord.Brand_use_in_hospital_cover_forecast__c + NewRecord.brand_use_in_hospital_cover_forecast__c;
                else
                    NeedRecord.Brand_use_in_hospital_cover_forecast__c = NewRecord.brand_use_in_hospital_cover_forecast__c;
                if(NeedRecord.HCP_evaluation_cover_actual__c > 0)
                    NeedRecord.HCP_evaluation_cover_actual__c = NeedRecord.HCP_evaluation_cover_actual__c + NewRecord.HCP_evaluation_cover_achieve__c;
                else
                    NeedRecord.HCP_evaluation_cover_actual__c = NewRecord.HCP_evaluation_cover_achieve__c;
                if(NeedRecord.HCP_evaluation_cover_forecast__c > 0)
                    NeedRecord.HCP_evaluation_cover_forecast__c = NeedRecord.HCP_evaluation_cover_forecast__c + NewRecord.HCP_evaluation_cover_forecast__c;
                else
                    NeedRecord.HCP_evaluation_cover_forecast__c = NewRecord.HCP_evaluation_cover_forecast__c;
                if(NeedRecord.P_class_cover_actual__c > 0)     
                    NeedRecord.P_class_cover_actual__c = NeedRecord.P_class_cover_actual__c + NewRecord.P_class_cover_achieve__c;
                else
                    NeedRecord.P_class_cover_actual__c = NewRecord.P_class_cover_achieve__c;
                if(NeedRecord.P_class_cover_forecast__c > 0)
                   NeedRecord.P_class_cover_forecast__c = NeedRecord.P_class_cover_forecast__c + NewRecord.P_class_cover_forecast__c;
                else
                   NeedRecord.P_class_cover_forecast__c = NewRecord.P_class_cover_forecast__c;
                if(NeedRecord.total_conversion_actual__c > 0)
                   NeedRecord.total_conversion_actual__c = NeedRecord.total_conversion_actual__c + NewRecord.Total_conversion_achieve__c;
                else
                    NeedRecord.total_conversion_actual__c = NewRecord.Total_conversion_achieve__c;
                if(NeedRecord.total_conversion_forecast__c > 0)
                    NeedRecord.total_conversion_forecast__c = NeedRecord.total_conversion_forecast__c + NewRecord.total_conversion_forecast__c;
                else
                    NeedRecord.total_conversion_forecast__c = NewRecord.total_conversion_forecast__c;          
                
                update NeedRecord;
            }
        }
    }
    if(system.trigger.isUpdate)
    {
        if((Trigger.new.size() == 1)&&(Trigger.new[0].Month__c != Month)){
            Achieve__c UNewRecord = Trigger.new[0];
            Achieve__c UOldRecord = Trigger.old[0];
            List<MedicalDashboard__c> UNeedRecordL = [Select Id,Brand_use_in_hospital_cover_actual__c,Brand_use_in_hospital_cover_forecast__c,HCP_evaluation_cover_actual__c,
                HCP_evaluation_cover_forecast__c,P_class_cover_actual__c,P_class_cover_forecast__c,total_conversion_actual__c,
                total_conversion_forecast__c,year__c,month__c From MedicalDashboard__c Where ownerid =: Trigger.new[0].ownerid and year__c =: Trigger.new[0].Year__c and month__c =: Trigger.new[0].Month__c];
           
           if(UNeedRecordL.size() > 0){
                MedicalDashboard__c UNeedRecord = UNeedRecordL.get(0);
                if(UNeedRecord.Brand_use_in_hospital_cover_actual__c > 0)
                    UNeedRecord.Brand_use_in_hospital_cover_actual__c = UNeedRecord.Brand_use_in_hospital_cover_actual__c + UNewRecord.Brand_use_in_hospital_cover_achieve__c - UOldRecord.Brand_use_in_hospital_cover_achieve__c;
                else
                    UNeedRecord.Brand_use_in_hospital_cover_actual__c =  UNewRecord.Brand_use_in_hospital_cover_achieve__c - UOldRecord.Brand_use_in_hospital_cover_achieve__c;
                if(UNeedRecord.Brand_use_in_hospital_cover_forecast__c > 0)
                    UNeedRecord.Brand_use_in_hospital_cover_forecast__c = UNeedRecord.Brand_use_in_hospital_cover_forecast__c + UNewRecord.brand_use_in_hospital_cover_forecast__c - UOldRecord.brand_use_in_hospital_cover_forecast__c;
                else
                    UNeedRecord.Brand_use_in_hospital_cover_forecast__c = UNewRecord.brand_use_in_hospital_cover_forecast__c - UOldRecord.brand_use_in_hospital_cover_forecast__c;
                if(UNeedRecord.HCP_evaluation_cover_actual__c > 0)
                    UNeedRecord.HCP_evaluation_cover_actual__c = UNeedRecord.HCP_evaluation_cover_actual__c + UNewRecord.HCP_evaluation_cover_achieve__c - UOldRecord.HCP_evaluation_cover_achieve__c;
                else
                    UNeedRecord.HCP_evaluation_cover_actual__c = UNewRecord.HCP_evaluation_cover_achieve__c - UOldRecord.HCP_evaluation_cover_achieve__c;
                if(UNeedRecord.HCP_evaluation_cover_forecast__c > 0)
                    UNeedRecord.HCP_evaluation_cover_forecast__c = UNeedRecord.HCP_evaluation_cover_forecast__c + UNewRecord.HCP_evaluation_cover_forecast__c - UOldRecord.HCP_evaluation_cover_forecast__c;
                else
                    UNeedRecord.HCP_evaluation_cover_forecast__c = UNewRecord.HCP_evaluation_cover_forecast__c - UOldRecord.HCP_evaluation_cover_forecast__c;
                if(UNeedRecord.P_class_cover_actual__c > 0)     
                    UNeedRecord.P_class_cover_actual__c = UNeedRecord.P_class_cover_actual__c + UNewRecord.P_class_cover_achieve__c - UOldRecord.P_class_cover_achieve__c;
                else
                    UNeedRecord.P_class_cover_actual__c = UNewRecord.P_class_cover_achieve__c - UOldRecord.P_class_cover_achieve__c;
                if(UNeedRecord.P_class_cover_forecast__c > 0)
                   UNeedRecord.P_class_cover_forecast__c = UNeedRecord.P_class_cover_forecast__c + UNewRecord.P_class_cover_forecast__c - UOldRecord.P_class_cover_forecast__c;
                else
                   UNeedRecord.P_class_cover_forecast__c = UNewRecord.P_class_cover_forecast__c - UOldRecord.P_class_cover_forecast__c;
                if(UNeedRecord.total_conversion_actual__c > 0)
                   UNeedRecord.total_conversion_actual__c = UNeedRecord.total_conversion_actual__c + UNewRecord.Total_conversion_achieve__c - UOldRecord.Total_conversion_achieve__c;
                else
                    UNeedRecord.total_conversion_actual__c = UNewRecord.Total_conversion_achieve__c - UOldRecord.Total_conversion_achieve__c;
                if(UNeedRecord.total_conversion_forecast__c > 0)
                    UNeedRecord.total_conversion_forecast__c = UNeedRecord.total_conversion_forecast__c + UNewRecord.total_conversion_forecast__c - UOldRecord.total_conversion_forecast__c;
                else
                    UNeedRecord.total_conversion_forecast__c = UNewRecord.total_conversion_forecast__c - UOldRecord.total_conversion_forecast__c;          
                
                update UNeedRecord;            
            }
        }
    }
    
    
    if(system.trigger.isDelete){
        if((Trigger.old.size() == 1)&&(Trigger.old[0].Month__c != Month)){
        Achieve__c DelRecord = Trigger.old[0];
        MedicalDashboard__c DNeedRecord = [Select Id,Brand_use_in_hospital_cover_actual__c,Brand_use_in_hospital_cover_forecast__c,HCP_evaluation_cover_actual__c,
                HCP_evaluation_cover_forecast__c,P_class_cover_actual__c,P_class_cover_forecast__c,total_conversion_actual__c,
                total_conversion_forecast__c,year__c,month__c From MedicalDashboard__c Where ownerid =: Trigger.old[0].ownerid and year__c =: Trigger.old[0].Year__c and month__c =: Trigger.old[0].Month__c];

            if(DNeedRecord.Brand_use_in_hospital_cover_actual__c > 0)
                DNeedRecord.Brand_use_in_hospital_cover_actual__c = DNeedRecord.Brand_use_in_hospital_cover_actual__c - DelRecord.Brand_use_in_hospital_cover_achieve__c;
            else
                DNeedRecord.Brand_use_in_hospital_cover_actual__c =  DelRecord.Brand_use_in_hospital_cover_achieve__c;
            if(DNeedRecord.Brand_use_in_hospital_cover_forecast__c > 0)
                DNeedRecord.Brand_use_in_hospital_cover_forecast__c = DNeedRecord.Brand_use_in_hospital_cover_forecast__c - DelRecord.brand_use_in_hospital_cover_forecast__c;
            else
                DNeedRecord.Brand_use_in_hospital_cover_forecast__c = DelRecord.brand_use_in_hospital_cover_forecast__c;
            if(DNeedRecord.HCP_evaluation_cover_actual__c > 0)
                DNeedRecord.HCP_evaluation_cover_actual__c = DNeedRecord.HCP_evaluation_cover_actual__c - DelRecord.HCP_evaluation_cover_achieve__c;
            else
                DNeedRecord.HCP_evaluation_cover_actual__c = DelRecord.HCP_evaluation_cover_achieve__c;
            if(DNeedRecord.HCP_evaluation_cover_forecast__c > 0)
                DNeedRecord.HCP_evaluation_cover_forecast__c = DNeedRecord.HCP_evaluation_cover_forecast__c - DelRecord.HCP_evaluation_cover_forecast__c;
            else
                DNeedRecord.HCP_evaluation_cover_forecast__c = DelRecord.HCP_evaluation_cover_forecast__c;
            if(DNeedRecord.P_class_cover_actual__c > 0)     
                DNeedRecord.P_class_cover_actual__c = DNeedRecord.P_class_cover_actual__c - DelRecord.P_class_cover_achieve__c;
            else
                DNeedRecord.P_class_cover_actual__c = DelRecord.P_class_cover_achieve__c;
            if(DNeedRecord.P_class_cover_forecast__c > 0)
               DNeedRecord.P_class_cover_forecast__c = DNeedRecord.P_class_cover_forecast__c - DelRecord.P_class_cover_forecast__c;
            else
               DNeedRecord.P_class_cover_forecast__c = DelRecord.P_class_cover_forecast__c;
            if(DNeedRecord.total_conversion_actual__c > 0)
               DNeedRecord.total_conversion_actual__c = DNeedRecord.total_conversion_actual__c - DelRecord.Total_conversion_achieve__c;
            else
                DNeedRecord.total_conversion_actual__c = DelRecord.Total_conversion_achieve__c;
            if(DNeedRecord.total_conversion_forecast__c > 0)
                DNeedRecord.total_conversion_forecast__c = DNeedRecord.total_conversion_forecast__c - DelRecord.total_conversion_forecast__c;
            else
                DNeedRecord.total_conversion_forecast__c = DelRecord.total_conversion_forecast__c; 

            update DNeedRecord;

        
        }    
    }
}